package com.ygo.feihua.Management;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import cn.bmob.v3.BmobRealTimeData;
import cn.bmob.v3.listener.ValueEventListener;
import org.json.JSONObject;
import org.json.JSONException;
import cn.bmob.v3.BmobQuery;
import com.ygo.feihua.BmobTable.MyUser;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.exception.BmobException;
import android.support.v4.util.ArrayMap;
import android.util.Log;

public class LiaotianManagement
{
	
	List<Map<String,Object>> sl=new ArrayList<Map<String,Object>>();
	private static LiaotianManagement ltm=new LiaotianManagement();
	private LiaotianListener ltl;
	private boolean isk=true;
	
	public final static int MESSAGE_TYPE_ADD=0;
	public final static int MESSAGE_TYPE_DEL=1;
	
	
	private LiaotianManagement(){

	}

	public void setLtl(LiaotianListener ltl)
	{
		this.ltl = ltl;
	}

	public LiaotianListener getLtl()
	{
		return ltl;
	}

	
	public void setSl(List<Map<String,Object>> sl)
	{
		sl.clear();
		for(Map<String,Object> ma:sl){
			sl.add(ma);
		}
	}

	public List<Map<String,Object>> getSl()
	{
		return sl;
	}

	public void addsl(Map<String,Object> se){
		
		
		if(sl.size() >= 500){
			for(int i = 0 ; i < 100 ; i++){
				sl.remove(i);
			}
		}
		sl.add(se);

		if(ltl!=null){
			ltl.ltsx(sl);
			ltl.adddel(MESSAGE_TYPE_ADD,se,sl.size()-1);

		}
	}
	public void delsl(Map<String,Object> se){
		for(int i=0;i<sl.size();i++){
			if(sl.get(i).get("id").equals(se.get("id"))){

				sl.remove(i);
				if(ltl!=null){
					ltl.adddel(MESSAGE_TYPE_DEL,se,i);
					ltl.ltsx(sl);
				}
				return;
			}
		}
	}

	public void startListener(){
		if(isk){
			isk=false;
		final BmobRealTimeData bmobRealTimeData=new BmobRealTimeData();
		bmobRealTimeData.start(new ValueEventListener() {

				@Override
				public void onConnectCompleted(Exception p1)
				{
					if(p1==null){
						bmobRealTimeData.subTableUpdate("GroupChatMessage");
					}else{
						Log.e("监听失败","失败原因"+p1);
						isk=true;
						ltl.listenerException(p1);
					}
					// TODO: Implement this method
				}


				@Override
				public void onDataChange(JSONObject json) {
					// TODO Auto-generated method stub
					JSONObject jsonObject = json;

					try
					{
						jsonObject = jsonObject.getJSONObject("data");

						final String id1=jsonObject.getString("userid");
						final String name1=jsonObject.getString("name");
						final String neirong1=jsonObject.getString("message");
						final String sj=jsonObject.getString("createdAt");
						BmobQuery<MyUser> query = new BmobQuery<MyUser>();
						query.getObject(id1, new QueryListener<MyUser>() {

								@Override
								public void done(MyUser p1, BmobException p2)
								{
									if(p2==null){
										Map<String,Object> ma=new ArrayMap<String,Object>();
										ma.put("id",id1);
										ma.put("name",name1);
										ma.put("neirong",neirong1);
										ma.put("sj",sj);
										ma.put("userqx",p1.getUser_qx());
										ma.put("tx",p1.getavatar());  
										ma.put("point",p1.getUserpoint());
										ma.put("user",p1);
										addsl(ma);
										//listView.setSelection(data.size() - 1);
									}
									// TODO: Implement this method
								}
							});
					}
					catch (JSONException e)
					{

					}
				}		
			});
		}
	}

	public static LiaotianManagement getdx(){

		return ltm;
	}
	
}
